from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .ser import *
from utils.comm import *
# Create your views here.

class Newss(APIView):
    def get(self,request):
        id =request.GET.get('id')
        puserid=request.GET.get('puserid')
        if id:
            ne = News.objects.filter(id=id).first()
            ser = NwesSer(ne)

            conn = get_redis()
            n = conn.hincrby(id, puserid)

            if n:
                print(type(n))
            if n>3:
                return Response({"code":10015})
            else:
                return Response({"code": 200, 'list': ser.data})
            # if n >= 3:
            #     return Response({"code": 10011,'list':ser.data})
            # else:
            #     conn.hincrby(id, puserid,1)
            # return Response({"code": 200, 'list': ser.data})
        else:
            datas = News.objects.all()
            ser = NwesSer(datas,many=True)
            return Response({"code":200,'list':ser.data})


class Userss(APIView):
    def post(self,request):
        username = request.data.get("username")
        password = request.data.get("password")
        name = Users.objects.filter(username=username).first()
        if name:
            if password == name.password:
                return Response({"code":200,"puserid":name.id})
            else:
                return Response({"code":10010})
        return Response({"code": 10011})
